package m202311.d20;

/**
 * @author haocongshun
 * @date 2023/11/20 19:03:47
 */

import java.util.LinkedList;
import java.util.StringJoiner;

/**
 * 6. N 字形变换
 */
public class Q6 {
    public static void main(String[] args) {
        System.out.println(new Q6().convert("ABC", 1));
    }

    public String convert(String s, int numRows) {
        StringBuilder[] chars = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            chars[i] = new StringBuilder();
        }
        int y = 0;
        boolean down = true;
        for (int i = 0; i < s.length(); i++) {
            if (down) {
                if (y < chars.length) {
                    chars[y].append(s.charAt(i));
                }
                if (y + 1 >= chars.length) {
                    down = false;
                    y = Math.max(0, y - 1);
                } else {
                    y++;
                }
                continue;
            }
            if (y >= 0) {
                chars[y].append(s.charAt(i));
                if (y == 0) {
                    y = Math.min(y + 1, numRows - 1);
                    down = true;
                } else {
                    y--;
                }
            }
        }
        return String.join("", chars);
    }
}
